package com.github.tetrisgame.model;

import com.github.tetrisgame.utils.Constants;

import java.util.Random;

public class Block {
    private int row = 0;
    private int column = 0;
    private int imageResource = 0;
    private BlockType type;
    private int[][] shape;
    private int imageResourceIndex;
    public Block(int row, int column) {
        this.row = row;
        this.column = column;
        imageResourceIndex = Constants.getRandomImageResIndex();
        imageResource = Constants.getFixedImageRes(imageResourceIndex);
        type = getRandomType();
        shape = BlockShape.getShape(type);
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[0].length; j++) {
                if (shape[i][j] != 0)shape[i][j] = imageResourceIndex;
            }
        }
    }
    private BlockType getRandomType(){
        BlockType[] types= BlockType.values();
        Random random = new Random();
        int index = random.nextInt(types.length);
        return types[index];
    }
    public int[][] getShape(){
        return shape;
    }

    public int getImageResource() {
        return imageResource;
    }

    public int getColumn() {
        return column;
    }
    public int getRow() {
        return row;
    }
    public void setRow(int row){
        this.row = row;
    }
    public void setColumn(int column){
        this.column = column;
    }

    public void rotate() {
        int n = shape.length;
        int[][] result = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[j][n - 1 - i] = shape[i][j];
            }
        }
        shape = result;
    }

    public enum BlockType{
        I,L,J,T,S,Z,O
    }
    public int getImageResourceIndex(){
        return imageResourceIndex;
    }
}
